categorian.onFirefoxLoad = function(event) {
  document.getElementById("contentAreaContextMenu")
          .addEventListener("popupshowing", function (e){ categorian.showFirefoxContextMenu(e); }, false);

  if (Application.extensions)  
      firstRun(extensions);  
  else  
      Application.getExtensions(firstRun);
};

categorian.showFirefoxContextMenu = function(event) {
  // show or hide the menuitem based on what the context menu is on
  // show on images
  document.getElementById("context-categorian").hidden = !(gContextMenu.onImage);
};

/** 
 * Installs the toolbar button with the given id {string} as last element
 * into the given toolbarId {string}, if it is not already present in the document.
 */
categorian.installButton = function(toolbarId, id) {    
    if (!document.getElementById(id)) {  
        var toolbar = document.getElementById(toolbarId);  
        var after = toolbar.lastChild.nextElementSibling; 
        toolbar.insertItem(id, after);
        toolbar.setAttribute('currentset', toolbar.currentSet);
        document.persist(toolbar.id, 'currentset');

        if (toolbarId == 'addon-bar')
            toolbar.collapsed = false;
    }
};

// Check if this is the first run and if so, install the button
function firstRun(extensions) {  
    var extension = extensions.get('categorian@wearegeek.nl');  
  
    if (extension.firstRun) {  
      categorian.installButton('nav-bar', 'categorian-toolbar-button'); 
    }  
}

// Eventlistener for initialisation on load
window.addEventListener('load', function () { categorian.onFirefoxLoad(); }, false);
